package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

public class CategoryController {


    @Autowired
    private CategoryService categoryService;

    //分类查询所有
    @GetMapping("/category/findAll")
    public ResultInfo findAll() {
        // 调用service查询
        List<Category> categoryList = categoryService.findAll();
        // 返回 结果
        return ResultInfo.success(categoryList);
    }

    //新增分类(菜品,套装公共后端)
    @PostMapping("/category")
    public ResultInfo save(@RequestBody Category category) {//接收参数
        //调用service添加
        categoryService.save(category);
        //添加成功返回结果
        return ResultInfo.success();
    }

    //修改菜品及套餐
    @PutMapping("/category")
    public ResultInfo updateById(@RequestBody Category category) {//接收参数
        //调用service修改
        categoryService.updateById(category);
        //返回修改结果
        return ResultInfo.success();

    }


    //删除菜单 (菜单有菜品不能删除)

    @DeleteMapping("/category")
    public ResultInfo deleteById(long id) {//接收参数
        // 调用service删除
        categoryService.deleteById(id);
        // 返回结果
        return ResultInfo.success();

    }


    //菜品管理 => 分类回显
    @GetMapping("/category/list")
    public ResultInfo findByType(Integer type) {
        // 根据类型查询分类列表
        List<Category> categoryList = categoryService.findByType(type);
        // 返回结果
        return ResultInfo.success(categoryList);
    }
}
